package cn.damai.mine.lambda.persontest;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.collections.MapUtils;

public class Person implements Serializable {

	/**
	 * Lambda表达式基本语法: (parameters) -> expression 参数 -> 表达式
	 * 
	 * 1. 不需要参数,返回值为 5 () -> 5
	 * 
	 * 2. 接收一个参数(数字类型),返回其2倍的值 x -> 2 * x
	 * 
	 * 3. 接受2个参数(数字),并返回他们的差值 (x, y) -> x – y
	 * 
	 * 4. 接收2个int型整数,返回他们的和 (int x, int y) -> x + y
	 * 
	 * 5. 接受一个 string 对象,并在控制台打印,不返回任何值(看起来像是返回void) (String s) ->
	 * System.out.print(s)
	 * 
	 */

	private static final long serialVersionUID = 1L;
	
	public final static String BLANK_STRING = "";

	public String name;
	public int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Person() {
		super();
	}

	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String toString() {
		return "name:" + name + " age:" + age;
	}

	static class Program {

		public static List<Person> PersonsList() {
			List<Person> persons = new ArrayList<Person>();
			for (int i = 0; i < 7; i++) {
				Person p = new Person("儿子" + i, 8 - i);
				persons.add(p);
			}
			return persons;
		}

		public static void main(String[] args) {

			List<Person> persons = PersonsList();

			System.out
					.println("*****************输出所有list对象*******************");
			persons.forEach(in -> System.out.println(in.toString()));// 使用
																		// lambda
																		// 表达式以及函数操作
			System.out.println();
			persons.forEach(System.out::println);// 在 Java 8 中使用双冒号操作符

			// age>6的集合
			List<Person> list2 = persons.stream().filter(ps -> ps.getAge() > 6)
					.collect(Collectors.toList());

			System.out
					.println("*****************输出age>6的对象*******************");
			list2.forEach(in -> System.out.println(in.toString()));

			System.out.println("*****************输出age>6的部分参数****************");
			list2.forEach(in -> System.out.println(in.getName() + ";"
					+ in.getAge()));

			System.out
					.println("*****************输出age>6的部分参数使用printf****************");
			list2.forEach(in -> System.out.printf("%s;%s", in.getName(),
					in.getAge()).println());

			System.out.println("**************合在一块输出对象并按字段age排序************");
			persons.stream().filter(n -> n.getAge() > 6)
					.sorted((a, b) -> a.getAge() - b.getAge())
					.forEach(c -> System.out.println(c.toString()));

			System.out.println("********输出age>6且按age排序的第一个对象********");
			Optional<Person> per = persons.stream().filter(n -> n.getAge() > 6)
					.sorted((a, b) -> a.getAge() - b.getAge()).findFirst();
			if (per.isPresent()) {
				Person son = per.get();
				System.out.println(son.getName() + ":" + son.getAge());
			}
			
			System.out.println("**********************filter**********************");
			
			List<Person> li = persons.stream().filter(person -> {
				if (person.getAge()==5)
					return true;//符合条件  将此对象放到collect里
				if (person.getAge()==7)
					return true;
				return false;
			}).collect(Collectors.toList());
			
			li.forEach(in -> System.out.println(in.toString()));

			System.out
					.println("################################################");

			List<String> names = Arrays.asList("Alice", "Bob", "Charlie",
					"Dave", "Hello");

			System.out.println("************输出所有String长度大于等于4的对象***********");
			List<String> filteredNames = names.stream()
					.filter(e -> e.length() >= 4).collect(Collectors.toList());
			filteredNames.forEach(fi -> System.out.println(fi));

			System.out.println("********输出所有String长度大于等于4的对象********");
			names.stream().filter(n -> n.length() > 4)
					.forEach(m -> System.out.println(m));

			System.out.println("********输出所有String长度大于等于4且最前边的两个对象********");
			names.stream().filter(n -> n.length() > 4).limit(2)
					.forEach(m -> System.out.println(m));
			
			
			System.out.println("**********************list转为map存储**********************");
			
			Map<String, Person> perMap = persons.stream().collect(
					Collectors.toMap(Person::getName, person -> person));
			//					key(person对象的getName输出)	value(person对象)
			
			
			Map<Integer, String> result1 = persons.stream().collect(
	                Collectors.toMap(Person::getAge, Person::getName));
			
			
			Map<String, String> result3 = persons.stream().collect(
	                Collectors.toMap(x -> x.getAge()+"", x -> x.getName()));
			
			
			
			//将map处理成   ?key=value&key=value&key=value形式  适用于url参数的连接
			Map<String, String> map = new HashMap<String, String>();
			String url = "";
			if(MapUtils.isNotEmpty(map)){
				String[] keyValues = map.entrySet()
						.stream()
						//处理map的value数据
						.map(entry -> entry.getKey() + "=" + StringUtils.defaultIfBlank(entry.getValue(), BLANK_STRING))
						.sorted()
					    .collect(Collectors.toList())
					    .toArray(new String[map.size()]);
				url = url +"?" + StringUtils.join(keyValues, "&");
	        }
			
			/*for (PayPlatformDto dto : payPlatforms) {
				List<PayGroup> subPayGroups = dto.getPayGroup().stream()
						.map(tGroup -> {
							PayGroup payGroup = new PayGroup();
							// 支付组id
							payGroup.setGroupId(tGroup.getGroupId());
							// 支付组名称
							payGroup.setGroupName(tGroup.getGroupName());
							payGroup.setPayColor("");
							return payGroup;
						}).collect(Collectors.toList());
				payGroups.addAll(subPayGroups);
			}*/
			
			

			System.out
					.println("**********************结束了**********************");
			
		}

	}

}
